#%% 引用相关的包
import cv2
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# from matplotlib import cm
from matplotlib import colors

#%% 绘制彩色三维散点图
img = cv2.imread('/Users/vine/Desktop/2021042806340001185G-J11200-5.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

r,g,b = cv2.split(img)
fig = plt.figure(figsize=(8,6),dpi=80)
axis = fig.add_subplot(1,1,1,projection="3d")

# 像素颜色设置
pixel_colors = img.reshape((np.shape(img)[0]*np.shape(img)[1],3))
# print(pixel_colors)
# 归一化
norm = colors.Normalize(vmin=-1.,vmax=1.)
norm.autoscale(pixel_colors)
# 转换成list
pixel_colors = norm(pixel_colors).tolist()
# print('*'*20)
# print(pixel_colors)

# 显示三维散点图
axis.scatter(r.flatten(),g.flatten(),b.flatten(),facecolors=pixel_colors,marker='.')
axis.set_xlabel("Red")
axis.set_ylabel("Green")
axis.set_zlabel("Bule")
plt.show()



#%% 在HSV中的图像生成彩色3D散点图

img = cv2.imread('/Users/vine/Desktop/2021042806340001185G-J11200-5.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
h,s,v = cv2.split(hsv_img)
fig = plt.figure(figsize=(8,6),dpi=80)
axis = fig.add_subplot(1,1,1,projection="3d")

# 像素颜色设置
pixel_colors = img.reshape((np.shape(img)[0]*np.shape(img)[1],3))
# print(pixel_colors)
# 归一化
norm = colors.Normalize(vmin=-1.,vmax=1.)
norm.autoscale(pixel_colors)
# 转换成list
pixel_colors = norm(pixel_colors).tolist()
# print('*'*20)
# print(pixel_colors)

# 显示三维散点图
axis.scatter(h.flatten(),s.flatten(),v.flatten(),facecolors=pixel_colors,marker='.')
axis.set_xlabel("hue")
axis.set_ylabel("saturation")
axis.set_zlabel("value")
plt.show()



#%%
import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('/Users/vine/Desktop/50.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
# cv2.imshow('sobel edge 1', hsv_img)

light_orange = (11,43,46)
dark_orange = (25,255,255)

light_white = (0,0,0)
dark_white = (180,255,46)

orange_mask = cv2.inRange(hsv_img,light_orange,dark_orange)
white_mask = cv2.inRange(hsv_img,light_white,dark_white)
mask = orange_mask+white_mask
# mask = white_mask

result = cv2.bitwise_and(img, img, mask=mask)

plt.subplot(121)
plt.imshow(mask,cmap="gray")
plt.subplot(122)
plt.imshow(result)
plt.show()

# cv2.imshow('img_yellow',mask)
cv2.imwrite('/Users/vine/Desktop/2mask.jpg', mask)
# cv2.waitKey()
# cv2.destroyAllWindows()

# %%
